package com.test.pattern.responsibility;

/**
 * @ClassName: Handler
 * @description:  抽象处理者（Handler）角色：定义一个处理请求的接口，包含抽象处理方法和一个后继连接。
 * @author: Mr du
 * @create: 2024-03-10 22:45
 */
public abstract class Handler {

    protected static final Integer NUM_ONE = 1;
    protected static final Integer NUM_THREE = 3;
    protected static final Integer NUM_SEVEN = 7;

    //该领导处理的请假天数区间
    protected int numStart;

    protected int numEnd;

    //声明上级领导
    private Handler nextHandler;

    public Handler(int numStart) {
        this.numStart = numStart;
    }

    public Handler(int numStart, int numEnd) {
        this.numStart = numStart;
        this.numEnd = numEnd;
    }

    //设置上级领导对象
    public void setNextHandler(Handler nextHandler) {
        this.nextHandler = nextHandler;
    }

    //各级领导处理请假条方法
    protected abstract void handleLeave(LeaveRequest leave);

    //提交请假条
    public final void submit(LeaveRequest leave){
        //如果请假天数小于等于0则直接结束流程
        if (leave.getNum() <= 0){
            return;
        }

        if (leave.getNum() >= this.numStart){
            //上级领导直接审批
            handleLeave(leave);

            //如果还有上级 并且请假天数超过了当前领导的处理范围
            if (this.nextHandler != null && leave.getNum() > numEnd){
                //再由更高级领导继续审批
                this.nextHandler.submit(leave);
            }else {
                System.out.println("流程结束");
            }
        }
    }
}
